package com.yun.baymax.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yun.baymax.R;
import com.yun.baymax.bean.adapter.Comment;
import com.yun.baymax.cache.UserCache;
import com.yun.baymax.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Administrator on 2017/6/12.
 */

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.MyHolder> {
    Context context;
    List<Comment> datas = new ArrayList<>();
    private LayoutInflater inflater;
    private String currentUserName;

    public CommentAdapter(Context context, List<Comment> datas) {
        this.context = context;
        this.datas = datas;
        inflater = LayoutInflater.from(context);
        currentUserName = UserCache.getInstance().getCurrentUser() == null ? null : UserCache.getInstance().getCurrentUser().getUserName();
    }

    public void setDatas(List<Comment> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_comment, parent, false);
        MyHolder myHolder = new MyHolder(view);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, final int position) {
        if (datas == null)
            return;
        final Comment comment = datas.get(position);
        if (!TextUtils.isEmpty(comment.user.getHeadImage()))
            Glide.with(context).load(comment.user.getHeadImage()).asBitmap().into(GlideUtils.setCircle(context, holder.ivAvatar));

        holder.tvName.setText(comment.user.getNick());
        holder.tvTime.setText(comment.getUpdatedAt());
        holder.tvComment.setText(comment.comment);
        if (comment.votes != null) {
            holder.tvVote.setText(String.valueOf(comment.votes.size()));
            holder.ivVote.setSelected(comment.votes.contains(currentUserName));
        } else {
            holder.tvVote.setText("");
            holder.ivVote.setSelected(false);
        }

        holder.ivVote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!holder.ivVote.isSelected()) {
                    holder.ivVote.setSelected(true);
                    comment.votes.add(currentUserName);
                    holder.tvVote.setText(String.valueOf(comment.votes.size()));
                } else {
                    holder.ivVote.setSelected(false);
                    comment.votes.remove(currentUserName);
                    holder.tvVote.setText(String.valueOf(comment.votes.size()));
                }
                updateComment(comment);
            }
        });
    }

    private void updateComment(Comment comment) {
        comment.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {

            }
        });
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        private ImageView ivAvatar;
        private TextView tvName;
        private TextView tvTime;
        private TextView tvComment;
        private TextView tvVote;
        private CheckBox ivVote;
        private int position;

        public MyHolder(View view) {
            super(view);
            ivAvatar = (ImageView) view.findViewById(R.id.iv_avatar);
            tvName = (TextView) view.findViewById(R.id.tv_name);
            tvTime = (TextView) view.findViewById(R.id.tv_time);
            tvVote = (TextView) view.findViewById(R.id.tv_vote);
            tvComment = (TextView) view.findViewById(R.id.tv_comment);
            ivVote = (CheckBox) view.findViewById(R.id.iv_vote);
        }
    }
}